package cn.fl.capital.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fl.capital.service.ICapPayInfoService;
import cn.fl.capital.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapClaimInfoQC;
import cn.fl.capital.constant.CapClaimInfoConstant;
import cn.fl.capital.constant.CapPayInfoConstant;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.service.ICapClaimDtlService;
import cn.fl.capital.service.ICapClaimInfoService;
import cn.fl.flow.service.IFlowService;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.ISplBankService;
import cn.fl.project.condition.PrjBscInfoQueryCondition;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.system.constant.SysLogConstant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowManageVQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.flow.facade.flow.dto.FlowStartedDTO;
import fl.flow.facade.flow.dto.SysFlowManageVDTO;

/**
 * @Description: 超时理赔记录表 控制层
 */
@Controller
@RequestMapping("/capital/capclaiminfo")
public class CapClaimInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CapClaimInfoController.class);

	/**
	 * CapClaimInfo 服务层BEAN
	 */
	@Autowired
	private ICapClaimInfoService capClaimInfoService;
	/**
	 * 付款流程记录 服务层BEAN
	 */
	@Autowired
	private ICapClaimDtlService capClaimDtlService;
    /**
     * PrdSplBank 服务层BEAN
     */
    @Autowired
    private ISplBankService prdSplBankService;
	/**
	 * 项目信息服务
	 */
	@Autowired
	private IPrjBscInfoService prjBscInfoService;

	/**
	 * 任务服务
	 */
	@Autowired
	private IFlowService flowService;
    @Autowired
    private IFlowApiService flowApiService;
	@Autowired
	private IFlowQueryApiService flowQueryApiService;
	@Autowired
	private ICapPayInfoService capPayInfoService;
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        //理赔状态
        List<KeyValueVO> claStsCdList = this.capClaimInfoService.getClaStsCdList();
		// 所属区域
		List<KeyValueVO> regionList = this.prjBscInfoService.getRegionList();
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		ModelAndView view = new ModelAndView("capital/capClaimInfo/capClaimInfoList");
		view.addObject("claStsCdList", JSON.toJSONString(claStsCdList));
		view.addObject("regionList", JSON.toJSONString(regionList));
		view.addObject("currentOrgCd", CurrentThreadContext.getCurrentOrgCd());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		ModelAndView view = new ModelAndView("capital/capClaimInfo/capClaimInfoAddFlow");
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载修改页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("capital/capClaimInfo/capClaimInfoModify");
		view.addObject("id", id);
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载修改.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET})
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", "加载例子详细页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("capital/capClaimInfo/capClaimInfoDetail");
		view.addObject("id", id);
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", " 加载例子详细页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        //设置查询条件？
        String splNm = request.getParameter("splNm");//供应商名称
        String cstMgrNm = request.getParameter("cstMgrNm");//客户经理
        String payAmtMin = request.getParameter("payAmtMin");//付款金额 小
        String payAmtMax = request.getParameter("payAmtMax");//付款金额 大
        String actPayTmBg = request.getParameter("actPayTmBg");//实际付款时间开始
        String actPayTmEd = request.getParameter("actPayTmEd");//实际付款时间结束
        String paySchNo = request.getParameter("paySchNo");//支付表号
        String buOrgCd = request.getParameter("buOrgCd");//事业部
        String cstNm = request.getParameter("cstNm");//客户名称
        String claStsCd = request.getParameter("claStsCd");//理赔状态
		String cstMgrRgnCd = request.getParameter("cstMgrRgnCd");// 所属区域
        CapClaimInfoQC qc = new CapClaimInfoQC();
		if (StringUtils.isNotEmpty(cstMgrRgnCd)) {
			qc.setCstMgrRgnCd(cstMgrRgnCd);//所属区域
		}
        if (StringUtils.isNotEmpty(splNm)) {
            qc.setSplNm(splNm);
        }
        if (StringUtils.isNotEmpty(cstMgrNm)) {
            qc.setCstMgrNm(cstMgrNm);
        }
        if (StringUtils.isNotEmpty(paySchNo)) {
            qc.setPaySchNo(paySchNo);
        }
        if (StringUtils.isNotEmpty(buOrgCd)) {
            qc.setBuOrgCd(buOrgCd);
        }
        if (StringUtils.isNotEmpty(cstNm)) {
            qc.setCstNm(cstNm);
        }
        if (StringUtils.isNotEmpty(actPayTmBg)) {
            qc.setActPayTmBg(actPayTmBg);
        }
        if (StringUtils.isNotEmpty(actPayTmEd)) {
            qc.setActPayTmEd(actPayTmEd);
        }
        if (StringUtils.isNotEmpty(payAmtMin)) {
            qc.setActPayAmtMin(DecimalUtils.convertYuanToFen(Double.valueOf(payAmtMin)));
        }
        if (StringUtils.isNotEmpty(payAmtMax)) {
            qc.setActPayAmtMax(DecimalUtils.convertYuanToFen(Double.valueOf(payAmtMax)));
        }
        if (StringUtils.isNotEmpty(claStsCd)) {
            qc.setClaStsCd(claStsCd);
        }

        page.setCondition(qc);

        BaseLogUtils.info(logger, "initList", "初始化超时理赔列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.capClaimInfoService.searchCapPayInfoListPage(page);
        List<CapClaimInfoVO> list = (List<CapClaimInfoVO>) pager.getResultList();
		Long payId = null;
		CapPayInfoVO capPayInfoVO = null;
        if(list != null && list.size() != 0){
			for (CapClaimInfoVO cap : list){
				payId = cap.getPayId();
				capPayInfoVO = this.capPayInfoService.selectById(payId);
				cap.setCstMgrRgnCdNm(capPayInfoVO.getCstMgrRgnCdNm());
			}
		}
        ListPageVO<CapClaimInfoVO> listPage = new ListPageVO<CapClaimInfoVO>(list, pager.getTotalCount());
        return listPage;
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		CapClaimInfoVO entity = null;
		try {
			entity = new CapClaimInfoVO();
			Long id = this.capClaimInfoService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增对象失败,name["+name+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "add", "新增对象失败,name["+name+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
//		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		CapClaimInfoVO entity = null;
		try {
			entity = new CapClaimInfoVO();
			entity.setId(Long.valueOf(id));
			boolean success = this.capClaimInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			this.capClaimInfoService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			CapClaimInfoVO vo = this.capClaimInfoService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 到流程附加信息页面
	 */
	@RequestMapping(value = "/toAdditionalInfoPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAdditionalInfoPage(HttpServletRequest request, HttpServletResponse response) {
		String claimId = request.getParameter("claimId");//超时理赔记录表ID
		String proIntId = request.getParameter("processInstanceId");//流程实例ID
		String taskId = request.getParameter("taskId");//任务ID
		String taskDefKey = request.getParameter("taskDefKey");//任务KEY
		String isDetail = request.getParameter("isDetail");//1:不可操作 0:可操作
		String taskNm = request.getParameter("taskNm");//任务名称
		String ctrlKey = request.getParameter("ctrlKey");//保存项控制字段1:保存分解信息2:保存实付金额及时间3:保存放款帐号信息
		if(StringUtils.isNotEmpty(proIntId)) proIntId = proIntId.replace("\'", "");
		if(StringUtils.isNotEmpty(taskId)) taskId = taskId.replace("\'", "");
		if(StringUtils.isNotEmpty(taskDefKey)) taskDefKey = taskDefKey.replace("\'", "");
		if(StringUtils.isNotEmpty(taskNm)) taskNm = taskNm.replace("\'", "");
		ModelAndView view = new ModelAndView("capital/capClaimInfo/audNodeAdditionalInfo");

		CapClaimInfoVO vo= this.capClaimInfoService.selectCapClaimInfoAndCapPayInfoById(Long.valueOf(claimId));
		List<KeyValueVO> payeeTypCdList = this.capClaimInfoService.getPayeeTypCdList();//收款方类型
		List<KeyValueVO> bkAccTypCdList = this.capClaimInfoService.getBkAccTypCdList();//收款账户类型
		List<KeyValueVO> payWayCdList = this.capClaimInfoService.getPayWayCdList();//付款方式类型

		view.addObject("payeeTypCdList",payeeTypCdList);
		view.addObject("bkAccTypCdList",bkAccTypCdList);
		view.addObject("payWayCdList",payWayCdList);
		view.addObject("claimInfo",vo);
		view.addObject("projectId", vo.getPrjId());
		view.addObject("proIntId", proIntId);
		view.addObject("taskId", taskId);
		view.addObject("taskDefKey", taskDefKey);
		view.addObject("taskNm", taskNm);
		view.addObject("isDetail", isDetail);
		view.addObject("ctrlKey", ctrlKey);

		return view;
	}


	/**
	 * 初始化超时理赔流程附加信息项目列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initProjectList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initProjectList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = "1";
        String pageSize = "10";
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        //设置查询条件？
        String prjId = request.getParameter("prjId");//项目ID
        PrjBscInfoQueryCondition qc = new PrjBscInfoQueryCondition();
        if (StringUtils.isNotEmpty(prjId)) {
        	List<Long> idList=new ArrayList<Long>();
        	idList.add(Long.valueOf(prjId));
        	qc.setIdList(idList);
        }else{
        	return null;
        }
        BaseLogUtils.info(logger, "initProjectList", "初始化超时理赔流程附加信息项目列表数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        qc.setUsrId(CurrentThreadContext.getCurrentUserId());
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
        page.setCondition(qc);
        Pager pager = this.prjBscInfoService.searchListPage(page);
        List<PrjBscInfoVO> list = (List<PrjBscInfoVO>)pager.getResultList();
        ListPageVO<PrjBscInfoVO> listPage = new ListPageVO<PrjBscInfoVO>(list, pager.getTotalCount());
        return listPage;
	}

	/**
	 * 初始化超时理赔流程附加信息准时放款流程列表
	 */
	@RequestMapping(value = "/initPayFlowList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initPayFlowList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = "1";
        String pageSize = "10";
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
        String processInstanceId = request.getParameter("processInstanceId");
        String claimId = request.getParameter("claimId");
        String payId = request.getParameter("payId"); 

        if(StringUtils.isEmpty(claimId)){
        	result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！"+ this.getRequestParameter(request));
			return result;
        }
    	List<CapClaimDtlVO> voList = this.capClaimDtlService.searchCapClaimDtlListByFlow(processInstanceId, Long.valueOf(claimId),Long.valueOf(payId));
    	if(CollectionUtils.isNotEmpty(voList)){
			result.setData(voList);
		}else{
			result.setData(new ArrayList<CapClaimDtlVO>());
		}

		return result;
	}
    /**
     * 启动流程
     */
    @RequestMapping(value = "/addFlowStart.do", method = { RequestMethod.POST})
    @ResponseBody
    public Object addFlowStart(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if(StringUtils.isEmpty(id)){
            //result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setSuccess(0);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "addFlowStart", SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY, CurrentThreadContext.getCurrentUserName());
            return result;
        }
        FlowStartParamsDTO flowStartParamsDTO = new FlowStartParamsDTO();
        try {
            //流程定义Key和流程定义ID 如果是前端选择的则直接指定,如果不是则系统自动按照配置选择唯一一条记录,如果找不到会提示
            logger.info("开始设置流程参数");
            SysFlowManageVQueryCondition flowManaQC = new SysFlowManageVQueryCondition();
            flowManaQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
            flowManaQC.setIsDel(BaseConstant.IS_YESNO_NO);
            flowManaQC.setIsValid(BaseConstant.IS_YESNO_YES);
            flowManaQC.setModelTyp(FlowConstant.FLOW_MODEL_TYP_CAPCLAIM_7);
            flowManaQC.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
            flowManaQC.setProcDefKey(CapClaimInfoConstant.CAP_PAY_FLOW_DEF_KEY);
            List<SysFlowManageVDTO> flowManageVListForStart = this.flowApiService.searchFlowManageVListForStart(flowManaQC);
            BaseLogUtils.info(logger,"addFlowStart","开始设置流程启动的参数"+id);
            SysFlowManageVDTO sysFlowManageVDTO = flowManageVListForStart.get(0);
            flowStartParamsDTO.setBusinessKey(id);//业务主键
            //流程定义Key和流程定义ID 如果是前端选择的则直接指定,如果不是则系统自动按照配置选择唯一一条记录,如果找不到会提示
            flowStartParamsDTO.setAudStsCd(FlowConstant.FLOW_CHECK_STATE_IN); //流程状态
            flowStartParamsDTO.setWordsInTheTable(CapPayInfoConstant.CAP_FLOW_S_H_CAP);
            flowStartParamsDTO.setProcDefKey(CapClaimInfoConstant.CAP_PAY_FLOW_DEF_KEY);//流程定义标识
            flowStartParamsDTO.setProcDefId(sysFlowManageVDTO.getProcDefId());//流程定义ID
            flowStartParamsDTO.setProcNo(sysFlowManageVDTO.getProcNo());//流程编号
            flowStartParamsDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            //flowStartParamsDTO.setBusinessKey(id);
            flowStartParamsDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
            flowStartParamsDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
            flowStartParamsDTO.setSysCd(CurrentThreadContext.getCurrentSysCd());
            flowStartParamsDTO.setInitiator(String.valueOf(CurrentThreadContext.getCurrentUserId()));
            flowStartParamsDTO.setInitiatorNm(CurrentThreadContext.getCurrentUserName());
            flowStartParamsDTO.setProcPriority(FlowConstant.FLOW_PRIORITY_EMERGENCY);//优先级,可以在界面定义
            flowStartParamsDTO.setBusinessType(FlowConstant.FLOW_BUSINESS_TYP_CAP_CLAIN_INFO);// 业务类型 到时候确定怎么定义
            flowStartParamsDTO.setModelTyp(FlowConstant.FLOW_MODEL_TYP_CAPCLAIM_7);//流程业务分类到时候确定
            flowStartParamsDTO.setSearchDesc(CurrentThreadContext.getCurrentOrgDispNm()+"、供应商流程大字段参数未定!");
            logger.info("开始发起超时理赔流程!");
            DataResultDTO dataResultDTO = this.prdSplBankService.addFlowStart(flowStartParamsDTO);
            if(!dataResultDTO.getCode().equals(BaseExceptionConstant.BASE_EXCEPTION_CODE_200)){
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
            }
            if(null!=dataResultDTO && null!=dataResultDTO.getData()){
                FlowStartedDTO flowStartedDTO = (FlowStartedDTO) dataResultDTO.getData();
                if(StringUtils.isNotBlank(flowStartedDTO.getNextOperUsrNames())){
                    result.setInfo("下一个节点操作人:"+flowStartedDTO.getNextOperUsrNames());
                }else{
                    result.setInfo("流程发起成功");
                }
            }
        } catch (BaseException e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(e.getMessage());
            BaseLogUtils.info(logger, "addFlowStart", "错误[BaseException],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" +e.getMessage(), CurrentThreadContext.getCurrentUserName());
            e.getStackTrace();
        } catch (Exception e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "addFlowStart", "错误[Exception],请求参数：" + JSONObject.toJSONString(flowStartParamsDTO) + " -ERROR:" +e.getMessage(), CurrentThreadContext.getCurrentUserName());
            e.getStackTrace();
        }
        return result;
    }
    /**
     * 到项目流程跟踪页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toCapClaimFlowPage.do", method = { RequestMethod.GET})
    public ModelAndView toCapClainFlowPage(HttpServletRequest request, HttpServletResponse response) {
        String capClaimId = request.getParameter("selecId");
        ModelAndView view = new ModelAndView("capital/capClaimInfo/capClaimInfoFlowList");
        view.addObject("capClaimId", capClaimId);
        BaseLogUtils.info(logger, "toCapClainFlowPage", "跳转到项目流程跟踪页面.", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
	 * 超时理赔流程附加页修改数据
	 */
    @RequestMapping(value = "/modifyCapClaimInfoByFlow.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object modifyCapClaimInfoByFlow(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "modifyCapClaimInfoByFlow", "超时理赔流程修改数据.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        String id = request.getParameter("id");
        String payId = request.getParameter("payId");//放款表ID
        String payeeTypCd = request.getParameter("payeeTypCd");//收款方类型
        String rcptUnt = request.getParameter("rcptUnt");//收款单位
        String bkAccTypCd = request.getParameter("bkAccTypCd");//账户类型
        String payeeBkNm = request.getParameter("payeeBkNm");//开户行
        String payeeBkNo = request.getParameter("payeeBkNo");//开户行号
        String accNo = request.getParameter("accNo");//收款帐号
        String claimAmt = request.getParameter("claimAmt");//应收金额
        String payWayCd = request.getParameter("payWayCd");//付款方式
        String payClaimAmt = request.getParameter("payClaimAmt");//实付金额
        String claimTm = request.getParameter("claimTm");//实付时间
        String ctrlKey = request.getParameter("ctrlKey");//保存项控制字段1:保存分解信息2:保存实付金额及时间3:保存放款帐号信息

        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功!");
        if(StringUtils.isEmpty(id)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("数据不存在!");
            BaseLogUtils.info(logger, "modifyCapClaimInfoByFlow", "超时理赔流程修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
            return result;
        }
        if(StringUtils.isEmpty(payId)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("参数不完整！"+ this.getRequestParameter(request));
            return result;
        }
        if(StringUtils.isEmpty(ctrlKey)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("参数不完整！"+ this.getRequestParameter(request));
            return result;
        }
        CapClaimInfoVO entity = null;
        List<CapPayeeInfoVO> capPayeeInfoVOList=null;
        CapPayeeInfoVO capPayeeInfoVO=null;
        try {
            capPayeeInfoVOList = new ArrayList<CapPayeeInfoVO>(1);
            capPayeeInfoVO = new CapPayeeInfoVO();
            entity = new CapClaimInfoVO();
            entity.setId(Long.valueOf(id));
            Double claimAmtDouble = Double.valueOf(claimAmt);
            entity.setClaimAmt(Long.valueOf(DecimalUtils.convertYuanToFen(claimAmtDouble)));
            entity.setPayId(Long.valueOf(payId));

            //理赔费用信息
            capPayeeInfoVO.setPayId(Long.valueOf(id));
            capPayeeInfoVO.setPayeeTypCd(payeeTypCd);
            capPayeeInfoVO.setRcptUnt(rcptUnt);
            capPayeeInfoVO.setBkAccTypCd(bkAccTypCd);
            capPayeeInfoVO.setPayeeBkNo(payeeBkNo);
            capPayeeInfoVO.setPayeeBkNm(payeeBkNm);
            capPayeeInfoVO.setAccNo(accNo);
            capPayeeInfoVO.setPayAmt(Long.valueOf(DecimalUtils.convertYuanToFen(claimAmtDouble)));
            capPayeeInfoVO.setPayWayCd(payWayCd);
            capPayeeInfoVO.setPurpCd(CapitalConstant.CODE_SPL_BK_PURP_CD_10);
            if(StringUtils.isNotEmpty(payClaimAmt)){
                Double payClaimAmtDouble = Double.valueOf(payClaimAmt);
                capPayeeInfoVO.setActPayAmt(Long.valueOf(DecimalUtils.convertYuanToFen(payClaimAmtDouble)));
            }
            if(StringUtils.isNotEmpty(claimTm)){
                capPayeeInfoVO.setActPayTm(DateUtils.formatStringToDate(claimTm+" 00:00:00", DateUtils.YYYY_MM_DDHHMMSS));
            }
            capPayeeInfoVOList.add(capPayeeInfoVO);

            //放款流程
            String FKprocessInstanceId = request.getParameter("FKprocessInstanceId");
            List<CapClaimDtlVO> capClaimDtlVOList = this.capClaimDtlService.searchCapClaimDtlListByFlow(FKprocessInstanceId, Long.valueOf(id),Long.valueOf(payId));
            if(CollectionUtils.isNotEmpty(capClaimDtlVOList)){
                for(CapClaimDtlVO capClaimDtlVO:capClaimDtlVOList){
                    String splitAmtStr=request.getParameter(capClaimDtlVO.getTaskDefKey()+"taskAmt");
                    if(StringUtils.isNotEmpty(splitAmtStr)){
                        capClaimDtlVO.setSplitAmt(Long.valueOf(splitAmtStr)*100);
                    }
                }
                entity.setCapClaimDtlVOList(capClaimDtlVOList);
            }
            entity.setPayeeList(capPayeeInfoVOList);
            boolean success = this.capClaimInfoService.modifyCapClaimInfoByFlow(entity);
            if(!success){
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("保存失败!");
                BaseLogUtils.info(logger, "modifyCapClaimInfoByFlow", "超时理赔流程修改数据失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            }
            BaseLogUtils.info(logger, "modifyCapClaimInfoByFlow", "超时理赔流程修改数据成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "modifyCapClaimInfoByFlow", "超时理赔流程修改数据失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
        }catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("系统错误,请联系管理员!");
            BaseLogUtils.error(logger, "modifyCapClaimInfoByFlow", "超时理赔流程修改数据失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
        }

        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "modifyCapClaimInfoByFlow", "超时理赔流程修改数据.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

        return result;
    }
    /**
     * 初始化项目流程跟踪列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/initCapClaimFlowList.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object initCapClaimFlowList(HttpServletRequest request, HttpServletResponse response) {
        String capClaimId = request.getParameter("capClaimId");//Id
        List<CapClaimInfoFlowSHVO> list = new ArrayList<CapClaimInfoFlowSHVO>();
        if(StringUtils.isNotEmpty(capClaimId)){
            list = this.capClaimInfoService.searchCapClaimFlowList(Long.valueOf(capClaimId));
            for (CapClaimInfoFlowSHVO capClaimInfoFlowSHVO : list) {
                if(StringUtils.isEmpty(capClaimInfoFlowSHVO.getGrpFlag())){
                    capClaimInfoFlowSHVO.setGrpFlag("-");
                }
            }
            Collections.sort(list, new Comparator<CapClaimInfoFlowSHVO>(){
                public int compare(CapClaimInfoFlowSHVO o1, CapClaimInfoFlowSHVO o2) {
                    if(o1.getGrpFlag().compareTo(o2.getGrpFlag())!=0){
                        return o1.getGrpFlag().compareTo(o2.getGrpFlag());
                    }
                    return o1.getBusTyp().compareTo(o2.getBusTyp());
                }
            });
        }
        ListPageVO<CapClaimInfoFlowSHVO> listPage = new ListPageVO<CapClaimInfoFlowSHVO>(list, list.size());
        return listPage;
    }
    
    /*******************************
	 * 选择收款方账号
	 * @param request
	 * @param response
	 * @return
	 *******************************/
	@RequestMapping(value = "/toChoosePayee.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toChoosePayee(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toChoosePayee", "加载toChoosePayee页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView mav = new ModelAndView("capital/capClaimInfo/choosePayeeList");
		return mav;
	}
}

